let flag = true
let timer
let loadtime
let num 
var endtime = window.localStorage.getItem('endtime')

// 点击发送验证码
$('.reglogCodebtn').on('click',function(){
    var Email = $('#inputEmail3')[0].value
    if(!Email||!flag)return
    // 保存结束时间
    window.localStorage.setItem('endtime', +new Date()+1000*60)
    gettimer()
    console.log(Email)
    sendAjax({
        method: 'post',             
        url: '/users/api/Email',
        data:{Email},
        success:res=>{
            console.log(res)
        }
    })
})

// 倒计时
function gettimer(){  
    flag = false
    timer = setInterval(function(){ 
        // 获取本地存储的url
        endtime = window.localStorage.getItem('endtime')
        loadtime = +new Date()
        num = parseInt((endtime - loadtime)/1000)
        num--
        if(num===0||num<0){
            flag = true
            $('.reglogCodebtn')[0].innerText = '获取验证码'
            $('.reglogCodebtn')[0].style.background = '#fff'
            localStorage.removeItem('endtime')
            clearTimeout(timer)
        }else{
            $('.reglogCodebtn')[0].innerText = num+'秒后重新获取'
            $('.reglogCodebtn')[0].style.background = 'rgba(222,222,222,.9)'
        }
    },1000)
}
if(endtime){
    gettimer()
}

// 表单验证 
$('.form-horizontal').validate({
rules: {
    reglogCode: {
        required: true,
    },
    email: {
        required: true,
        email: true
    }
},
messages: {
    reglogCode: {
        required: '请输入验证码',
    },
    email: {
        required: '请输入您的邮箱',
        email: '请填写正确的邮箱格式'
    }
},
submitHandler: function (form) {
    let vcatcode = getCookie('vcatcode')
    var reglogCode = $('#reglogCode')[0].value
    var email = $('#inputEmail3')[0].value
    sendAjax({
        method: 'post',             
        url: '/users/api/confirmcode',
        data: {reglogCode,vcatcode,email},
        success: res => {
        if(res.errorCode == 0){
            layer.msg('验证通过！',{
                icon:1,
                time:2000
            },function(){
                setCookie('userid',res.data.userid,600)
                localStorage.removeItem('endtime')
                clearTimeout(timer)                
                setformtable()
                formload()
            })
            }else{
            layer.msg('验证码错误，请重新确认！',{
                icon:2,
                time:2000

            })}
        }
    })
}
})

// 验证通过后新创建一个form表单覆盖
function setformtable(){
    $('body')[0].innerHTML = `
    <form class="form-load box">
        <div class="log">logo</div>
        <div class="login">
            <div class="form-group loadpass ">
                <label for="inputEmail3" class="col-sm-3 control-label loadpass1   span">新密码</label>
                <div class="col-sm-9">
                    <input type="password" name="password" class="form-control" id="inputEmail3" placeholder="请输入您的新密码">
                </div>
            </div>

            <div class="form-group loadpasss ">
                <label for="reglogCode" class="col-sm-3 control-label loadpassss span">确认密码</label>
                <div class="col-sm-9">
                    <input type="password" name="passwords" class="form-control" id="loadpasss"
                        placeholder="请再次确认您的新密码">
                </div>
            </div>

            <div class="form-group">
                <div class=" col-sm-12 loginbtnbox">
                    <button type="submit" class="loginbtn nextbtn">确认修改</button>
                </div>
            </div>

        </div>
    </form>


    `
}

// 密码找回
function formload(){
$('.form-load').validate({
    rules: {
        password: {
            required: true, 
            minlength: 6, 
            maxlength: 16, 
        },
        passwords: {
            required: true, 
            minlength: 6, 
            maxlength: 16, 
            equalTo: '#loadpasss'
        },
    },
    messages: {
        password: {
            required: '密码不能为空', 
            minlength: '长度最小6位', 
            maxlength: '长度最大16位', 

        },
        passwords: {
            required: '确认密码不能为空', 
            minlength: '长度最小6位', 
            maxlength: '长度最大16位', 
            equalTo: '两次密码必须一致！'
        }
    },
    submitHandler: function (form) {
        let formData = $('.form-load').serialize()
        let userid = getCookie('userid')
        sendAjax({
            method: 'post',             
            url: '/users/api/loadpasss/'+userid,
            data: formData,
            success: res => {
                let {
                    errorCode,
                    message
                } = res
                if (errorCode == 0) {
                    layer.msg("修改成功，请牢记您的新密码！", {
                        icon: 1,
                        time: 2000
                    },function(){
                        removeCookie('vcatcode')
                        location.href = '/login.html'
                    })
                } else {
                    layer.msg(message, {
                        icon: 2,
                        time: 2000
                    })
                }
            }
        })
    }
    })
}
